package com.ujiuye.service.impl;

import com.ujiuye.dao.Impl.StudentDaoImpl;
import com.ujiuye.model.Page;
import com.ujiuye.model.Student;
import com.ujiuye.service.StudentService;

import javax.servlet.annotation.MultipartConfig;
import java.util.List;

public class StudentServiceImpl implements StudentService {
    StudentDaoImpl sd = new StudentDaoImpl();

    @Override
    public boolean saveStudent(Student student) {
        //将Student对象中封装的数据交给数据交互层
        int row = sd.saveStudent(student);
        System.out.println(row);
        return row > 0? true : false;
    }

    @Override
    public Page<Student> stuPageLists(String currentPage, Student student) {
        //获取符合条件学生总条数
        long count = sd.getStuCount(student);
        //处理当前页数据
        int current = 1;
        if (currentPage != null && currentPage.trim() != "") {
            current = Integer.parseInt(currentPage);
        }
        //获取分页数据
        int size = 5; //每页显示的条数
        //用当前页减1乘以每页显示的条数
        int start = (current - 1) * size;
        List<Student> stuList = sd.selectStudentPage(start, size, student);
        //将获取的数据封装为page对象
        Page<Student> page = new Page<>(size, current, (int) count, stuList);

        return page;
    }

    @Override
    public int deleteStudentByIds(String ids) {
        int num = 0;
        if (ids != null && ids.trim() != "") {
            // 将学生编号列表拆分成单个学生编号的数组
            String[] sid = ids.split(",");

            for (String id : sid) {
                int idd = Integer.parseInt(id);
                // 调用dao层的方法
                num += sd.deleteStudentById(idd);
            }
        }
        return num;
    }

    @Override
    public Student updateStudentByIds(String id) {
        if(id != null && id.trim() != ""){
            int sid = Integer.parseInt(id);
            Student student = sd.getStudentById(sid);
            return student;
        }
        return null;
    }

    @Override
    public boolean updateStudentDo(Student student) {
        int row = sd.updateStudentDo(student);
        return false;
    }
}
